﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using MDF.Logger.Managements;

namespace MDF.Worker
{
    public abstract class IWorker
    {
        public string WorkerName { get; set; }
        private Thread m_Thread = null;

        public Thread ThreadToRun
        {
            get { return GetThread(); }
        }
        public onWorkerDoneHandler onDoneEvent = null;
        protected abstract object DoIt();
        private void Run()
        {
            try
            {
                LogManager.GetInstance().Info(this.WorkerName + ": Starting ... ");
                var objResult = this.DoIt();
                if (onDoneEvent != null)
                {
                    onDoneEvent(objResult);
                }
                else
                {
                    LogManager.GetInstance().Warning(this.WorkerName + ": Not have callback to receicve result");
                }
                LogManager.GetInstance().Info(this.WorkerName + ": Done");
            }
            catch (Exception ex)
            {
                LogManager.GetInstance().Error(this.WorkerName,ex);
            }
        }
        private Thread GetThread()
        {
            if (m_Thread == null)
            {
                m_Thread = new Thread(Run);
            }
            return m_Thread;
        }
    }
}
